using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Audio;

namespace Utils.Audio
{
    public class Sound
    {
        SoundEffect soundEffect;
        SoundEffectInstance[] soundInstances;
        float masterVolume;
        int current;

        public Sound(SoundEffect soundEffect, int instances, float masterVolume)
        {
            this.soundEffect = soundEffect;
            this.masterVolume = masterVolume;
            soundInstances = new SoundEffectInstance[instances];

            for (int i = 0; i < instances; i++)
            {
                soundInstances[i] = soundEffect.CreateInstance();
                soundInstances[i].Volume = masterVolume;
            }
        }

        public void Play()
        {
            if (soundInstances[current].State == SoundState.Stopped)
            {
                soundInstances[current].Play();

                current = (current + 1) % soundInstances.Count();
            }
        }

        public void PlayInstance(int instance)
        {
            if (soundInstances[instance].State != SoundState.Playing)
            {
                soundInstances[instance].Play();
            }
        }
    }
}
